﻿using Nest;

namespace elasticsearchDemo.Model
{
    public abstract class ElasticsearchEntity
    {
        private Guid? _id;

        public Guid Id
        {
            get
            {
                _id ??= Guid.NewGuid();
                return _id.Value;
            }
            set => _id = value;
        }

        //private long? _timestamp;

        //[Number(NumberType.Long, Name = "timestamp")]
        //public long Timestamp
        //{
        //    get
        //    {
        //        _timestamp ??= GetTimeStamp();
        //        return _timestamp.Value;
        //    }
        //    set => _timestamp = value;
        //}
        /// <summary>
        /// 生成时间戳 
        /// </summary>
        /// <returns>当前时间减去 1970-01-01 00.00.00 得到的毫秒数</returns>
        public long GetTimeStamp()
        {
            DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0));
            DateTime nowTime = DateTime.Now;
            long unixTime = (long)System.Math.Round((nowTime - startTime).TotalMilliseconds, MidpointRounding.AwayFromZero);
            return unixTime;
        }
    }
}
